<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <style>
        .modal {
            position: fixed;
            width: 200px;
            height: 200px;
            line-height: 200px;
            background-color: yellowgreen;
            text-align: center;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
    <!-- <div class="modal">
        登录对话框
    </div> -->
    <button id="open">打开</button>
    <button id="close">关闭</button>
    <script>
        const Modal = (function () {
            let instance = null;
            return function () {
                if (!instance) {
                    instance = document.createElement('div')
                    instance.innerHTML = "登录对话框"
                    instance.className = "modal"
                    instance.style.display = "none"
                    document.body.appendChild(instance)
                }
                return instance
            }
        })()
        document.querySelector('#open').onclick = function () {
            // 创建modal
            const modal = Modal()
            // 显示modal
            modal.style.display = "block"
        }
        document.querySelector('#close').onclick = function () {
            const modal = Modal() // 拿到的是唯一的
            // 取消modal
            modal.style.display = "none"
        } 
    </script>
</body>

</html>